<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改用户信息</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 500px;
            margin: 0 auto;
            padding: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            cursor: pointer;
        }
        #message {
            margin-top: 15px;
            padding: 10px;
            display: none;
        }
    </style>
</head>
<body>
    <h1>修改用户信息</h1>

    <div class="form-group">
        <input type="hidden" id="id">
    </div>
    <div class="form-group">
        <label for="userName">用户名:</label>
        <input type="text" id="userName" required>
    </div>

    <div class="form-group">
        <label for="password">密码:</label>
        <input type="password" id="password" required>
    </div>

    <div class="form-group">
        <label for="githubUrl">GitHub地址:</label>
        <input type="url" id="githubUrl">
    </div>

    <button onclick="updateUser()">提交修改</button>

    <div id="message"></div>

    <script src="js/jquery.min.js"></script>
    <script src="js/common.js"></script>
    <script type="text/javascript">
        function getLoginUserInfo(){
            $.ajax({
                type:"get",
                url:"user/getLoginUserInfo",
                success:function (result){
                    console.log(result);
                    if (result.code === "SUCCESS" && result.data != null){
                        let userInfo = result.data;
                        $("#id").val(userInfo.id);
                        $("#userName").val(userInfo.userName);
                        $("#githubUrl").val(userInfo.githubUrl);
                    }else {
                        alert(result.errorMessage);
                    }
                },error:function (error){
                    if (error != null && error.status === 401){
                        location.href = "blog_login.html";
                    }
                }
            })
        }
        getLoginUserInfo();
        function updateUser() {
            $.ajax({
                type: "post",
                url: "user/modify",
                data:{
                    id:$("#id").val(),
                    userName:$("#userName").val(),
                    password:$("#password").val(),
                    githubUrl:$("#githubUrl").val()
                },
                success:function (result){
                    if (result.code === "SUCCESS" && result.data === "修改成功"){
                        alert("修改成功");
                        location.href = "blog_list.html";
                    }else {
                        alert(result.errorMessage);
                    }
                },error:function (error){
                    if (error != null && error.status === 401){
                        location.href = "blog_login.html";
                    }
                }
            })
        }
    </script>
</body>
</html>